package com.es.main;

import com.es.action.classloader.ClassLoaderEx;
import com.es.action.heat.exec.exectime.ExecFactory;
import com.es.action.heat.filechange.FileMonitor;
import com.es.common.PreCache;

import java.util.Map;

/**
 * 客户端程序
 *
 * @author Will
 */
public class ClientSide {

    public Object run(String className, String methodName) {
        return run(className, methodName, new Class<?>[]{}, new Object[]{});
    }

    public Object run(String className, String methodName, Class<?>[] parameterTypes, Object[] args) {
        PreCache.setCacheNoneConfig();

        // 最新的文件状态
        Map<String, Long> allFileState = FileMonitor.getAllFileState();
        if (allFileState.size() < 1) {
            return null;
        }

        // 编译
        new ExecFactory().run(allFileState);
        try {
            // 必须要有无参构造
            Object o = new ClassLoaderEx().findClass(className).newInstance();
            return o.getClass().getDeclaredMethod(methodName, parameterTypes).invoke(o, args);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return null;
    }
}
